<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . "/../database/$database/database.inc.php";
  
header('Content-Type: text/plain');

global $myManager;

$action = MiscUtils::getParam('action', NULL);

$myManager->start_t();
try {
  switch ($action) {
    case 'login':
      login($myManager);
      break;
    case 'customers':
      customers($myManager);
      break;
    case 'customer':
      customer($myManager);
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}
$myManager->commit_t();

function login($myManager) {
  $orm = classToOrm('user');
  $account = MiscUtils::getParam('a', NULL);
  $password = MiscUtils::getParam('p', START);

  $result = $orm->find($myManager, NULL, NULL, NULL, NULL, "d.account = '$account' AND d.password = '$password'", NULL);
  if (count($result->data) == 1) {
    $user = $result->data[0];
    $_SESSION[USER] = $user;
    
    setcookie('user', json_encode($user), (time() + 3600), PATH, DOMAIN);
    
    echo json_encode($user);
  } else {
    echo json_encode(array());
  }
}

function customers($myManager) {
  try {
    $host = "127.0.0.1";
    $database = "dlmanageny";
    $user = "dlmanageny";
    $password = "weida911";
    $pdo = new PDO("mysql:host=$host;dbname=$database", "$user", "$password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $joint = MiscUtils::getParam('j', NULL);
    $condition = MiscUtils::getParam('c', NULL);
    $order = MiscUtils::getParam('o', 'c.e_oid');
    $queue = MiscUtils::getParam('q', 'ASC');
    $page = MiscUtils::getParam('p', START);
    $size = MiscUtils::getParam('s', SIZE);
    $pageSkip = ($page - 1) * $size;
    $total = 0;
    $objects = array();
    

    $next = false;
    $stmt = $pdo->query("SHOW TABLES");
    while ($row = $stmt->fetch()) {
      if ($row[0] == 'ns_customer') {
        $next = true;
      }
    }
    
    if ($next == true) {
      $header = "SELECT DISTINCT c.e_oid AS id FROM ns_customer AS c";
      $sql = $header . (($joint) ?  " $joint" : '') . (($condition) ? " WHERE $condition" : '') . " ORDER BY $order $queue LIMIT $pageSkip, $size";
      
      $orm = classToOrm('customer');
      $epObjects = array();
      
      
      $stmt = $pdo->query($sql);
      while ($row = $stmt->fetch()) {
        $epObjects = $myManager->find("FROM Customer AS d WHERE d.oid = ?", $row['id']);
        foreach ($epObjects as &$epObject) {
          $object = new stdClass();
          $object = $orm->__toObject($myManager, $epObject, $object);
          $objects[] = $object;
        }
      }
      
      $sql = "SELECT COUNT(DISTINCT c.e_oid) AS total FROM ns_customer AS c" . (($joint) ?  " $joint" : '') . (($condition) ? " WHERE $condition" : '');
      
      $stmt = $pdo->query($sql);
      while ($row = $stmt->fetch()) {
        $total = $row['total'];
      }
    }
    
    $result = new stdClass();
    $result->data = $objects;
    $result->page = $page;
    $result->size = $size;
    $result->order = $order;
    $result->queue = $queue;
    $result->joint = $joint;
    $result->condition = $condition;
    $result->total = $total;
    echo json_encode($result);

  } catch (PDOException $e) {
    echo "Failed: " . $e->getMessage() . "\n";
    exit;
  }
  
  unset($pdo);
  unset($query);
}

function customer($myManager) {
  $orm = classToOrm('customer');
  $id = MiscUtils::getParam('i', NULL);

  $result = $orm->find($myManager, NULL, NULL, NULL, NULL, "d.oid = '$id'", NULL);
  if (count($result->data) == 1) {
    $customer = $result->data[0];
    $_SESSION[USER] = $customer;
    
    echo json_encode($customer);
  } else {
    echo json_encode(array());
  }
}
?>